﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using BusinessLogic;
using System.Web.Security;
using System.Data.SqlClient;

namespace DataAccessLogic
{
    public static class PartDataAccess
    {
        private static Part BindRowToPart(DataRow row)
        {
            Part p = new Part();            
            p.ID = DataAccess.CheckForGuidColumn(row, "ID");            
            p.Description = DataAccess.CheckForStringColumn(row, "DESCRIPTION");
            p.Number = DataAccess.CheckForStringColumn(row, "PARTNUMBER");
            p.Price = DataAccess.CheckForDecimalColumn(row, "PRICE");
            p.DateAdded = DataAccess.CheckForDateTimeColumn(row, "DATEADDED");
            p.DateChanged = DataAccess.CheckForDateTimeColumn(row, "DATECHANGED");
            p.AddedByID = DataAccess.CheckForGuidColumn(row, "ADDEDBYID");
            p.ChangedByID = DataAccess.CheckForGuidColumn(row, "CHANGEDBYID");
            
            return p;
        }

        public static Guid Create(Part part)
        {
            Guid id = Guid.Empty;
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            string outputParameterName = "@ID";

            if (part != null)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                SqlParameter p = new SqlParameter();
                p.ParameterName = outputParameterName;
                p.DbType = System.Data.DbType.Guid;
                p.Direction = System.Data.ParameterDirection.Output;

                parms.Add(p);

                parms.Add(new SqlParameter("@DESCRIPTION", part.Description));
                parms.Add(new SqlParameter("@PARTNUMBER", part.Number));
                parms.Add(new SqlParameter("@PRICE", part.Price));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", providerUserKey));

                object obj = DataAccess.Create(StoredProcedures.USP_PART_CREATE, outputParameterName, parms);

                if (obj != null)
                {
                    id = new Guid(obj.ToString());
                }
            }

            return id;
        }

        private static List<Part> Get(string storedProcedure)
        {
            return Get(storedProcedure, null);
        }

        private static List<Part> Get(string storedProcedure, List<SqlParameter> parms)
        {
            List<Part> parts = null;

            DataTable dt = DataAccess.Get(storedProcedure, parms);

            if (null != dt)
            {
                parts = new List<Part>();

                foreach (DataRow row in dt.Rows)
                {
                    parts.Add(BindRowToPart(row));
                }
            }

            return parts;
        }

        public static List<Part> GetAll()
        {
            return Get(StoredProcedures.USP_PART_GETALL);
        }

        public static Part GetByID(Guid id)
        {
            Part part = null;

            if (id != Guid.Empty)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                parms.Add(new SqlParameter("@ID", id));

                List<Part> i = Get(StoredProcedures.USP_PART_GETBYID, parms);

                if (i != null && i.Count > 0)
                {
                    part = i[0];
                }
            }

            return part;
        }        

        public static DataTable SearchAll()
        {
            return DataAccess.Get(StoredProcedures.USP_PART_SEARCH);
        }
    }
}